package com.springexample.demo.controller;

import com.springexample.demo.bean.User;
import com.springexample.demo.component.Response;
import com.springexample.demo.exception.ParamNullException;
import com.springexample.demo.service.ConfigurationHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = {"测试Controller"})
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private List<ConfigurationHandler> configurationList;

    @ApiOperation("除法运算")
    @GetMapping("/division/{num1}/{num2}")
    public Response division(@PathVariable @ApiParam("数字一") int num1,
                        @PathVariable @ApiParam("数字二") int num2) {
        if(num2==0){
            throw new RuntimeException("运行时异常");
        }
        return Response.sucess(num1/num2);
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/getUserList")
    public Response getUserList() {
        List<User> userList = new ArrayList<>();
        userList.add(User.builder().name("张三").age(20).gender("man").build());
        userList.add(User.builder().name("李四").age(30).gender("women").build());
        return Response.sucess(userList);
    }

    @ApiOperation("获取配置信息")
    @GetMapping("/getConfig")
    public Response getConfig(){
        StringBuilder val = new StringBuilder();
        configurationList.forEach(c->{
            val.append(c.getValue());
        });
        return Response.sucess(val);
    }

    @ApiOperation("手动注销Bean")
    @GetMapping("/unRegisterBean")
    public Response unRegisterBean(){
        StringBuilder val = new StringBuilder();
        configurationList.forEach(c-> val.append(c.unRegisterBean()));
        return Response.sucess(val);
    }

}
